package com.rayboot.footpoint.provider;

import com.rayboot.footpoint.R;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class CategoryProvider implements ISubProvider {
	private static final Uri BaseURI = Uri
			.parse("content://com.rayboot.footpoint.provider.FootprintContentProvider/category");
	private static final String TABLE = "category";
	private static final String TAG = "CategoryProvider";
	private Context mContext;
	private SQLiteDatabase mDB;

	private void initData(SQLiteDatabase paramSQLiteDatabase) {
		paramSQLiteDatabase
				.execSQL("INSERT INTO category(keyword,name,icon_id) values('favorite','Favorites'," + R.drawable.icon_favorites  + ")");
		paramSQLiteDatabase
				.execSQL("INSERT INTO category(keyword,name,icon_id) values('shopping','Shopping'," +R.drawable.icon_shopping + ")");
		paramSQLiteDatabase
				.execSQL("INSERT INTO category(keyword,name,icon_id) values('dining','Dining'," +R.drawable.icon_restaurant + ")");
		paramSQLiteDatabase
				.execSQL("INSERT INTO category(keyword,name,icon_id) values('leisure','Leisure'," +R.drawable.icon_leisure + ")");
		paramSQLiteDatabase
				.execSQL("INSERT INTO category(keyword,name,icon_id) values('other','Others'," +R.drawable.icon_others + ")");
	}

	public int delete(Uri paramUri, String paramString,
			String[] paramArrayOfString) {
		if (CategoryColumns.CONTENT_URI.equals(paramUri))
			return this.mDB.delete(TABLE, paramString, paramArrayOfString);
		throw new IllegalArgumentException("Unknown URL " + paramUri);
	}

	public Uri getBaseURI() {
		return BaseURI;
	}

	public String getType(Uri paramUri) {
		return "";
	}

	public Uri insert(Uri paramUri, ContentValues paramContentValues) {
		return null;
		// if (paramContentValues != null)
		// ;
		// for (ContentValues localContentValues = new ContentValues(
		// paramContentValues); !CategoryColumns.CONTENT_URI
		// .equals(paramUri); localContentValues = new ContentValues())
		// throw new IllegalArgumentException("Unknown URL " + paramUri);
		// if (!localContentValues.containsKey("keyword"))
		// throw new SQLException("Failed to insert row into " + paramUri
		// + " (null keyword)");
		// if (!localContentValues.containsKey("name"))
		// throw new SQLException("Failed to insert row into " + paramUri
		// + " (null group)");
		// if (!localContentValues.containsKey("icon_id"))
		// throw new SQLException("Failed to insert row into " + paramUri
		// + " (null icon_id)");
		// long l = this.mDB.replace(TABLE, "keyword", localContentValues);
		// if (l > 0L)
		// return ContentUris.withAppendedId(CategoryColumns.CONTENT_URI, l);
		// throw new SQLException("Failed to insert row into " + paramUri);
	}

	public void onCreate(SQLiteDatabase paramSQLiteDatabase) {
		paramSQLiteDatabase
				.execSQL("CREATE TABLE category (keyword TEXT primary key ,name TEXT ,icon_id integer);");
		initData(paramSQLiteDatabase);
	}

	public void onDrop(SQLiteDatabase paramSQLiteDatabase) {
		paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS category");
	}

	public Cursor query(Uri paramUri, String[] paramArrayOfString1,
			String paramString1, String[] paramArrayOfString2,
			String paramString2) {
		SQLiteQueryBuilder localSQLiteQueryBuilder = new SQLiteQueryBuilder();
		if (CategoryColumns.CONTENT_URI.equals(paramUri)) {
			localSQLiteQueryBuilder.setTables(TABLE);
			return localSQLiteQueryBuilder.query(this.mDB, paramArrayOfString1,
					paramString1, paramArrayOfString2, null, null, null);
		}
		throw new IllegalArgumentException("Unknown URL " + paramUri);
	}

	public void setContext(Context paramContext) {
		this.mContext = paramContext;
	}

	public void setDB(SQLiteDatabase paramSQLiteDatabase) {
		this.mDB = paramSQLiteDatabase;
	}

	public int update(Uri paramUri, ContentValues paramContentValues,
			String paramString, String[] paramArrayOfString) {
		if (CategoryColumns.CONTENT_URI.equals(paramUri))
			return this.mDB.update(TABLE, paramContentValues, paramString,
					paramArrayOfString);
		throw new IllegalArgumentException("Unknown URL " + paramUri);
	}

	public static final class CategoryColumns {
		public static final Uri CONTENT_URI = CategoryProvider.BaseURI;
		public static final String ICON_ID = "icon_id";
		public static final String KEYWORD = "keyword";
		public static final String NAME = "name";
	}
}
